package com.javachmapi.springbootemail.controller;

import com.javachmapi.springbootemail.config.SendEmailImpl;
import com.javachmapi.springbootemail.dao.SysNoticeDao;
import com.javachmapi.springbootemail.entity.SysNotice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (SysEmail)表控制层
 *
 * @author 宋利军
 * @since 2020-05-10 12:05:48
 */
@Slf4j
@RestController
@RequestMapping("sysEmail")
public class SysEmailController {

    @Resource
    private SysNoticeDao sysNoticeDao;

    @Resource
    private SendEmailImpl sendEmailImpl;

    private static String is_true = "true";
    private static String is_false = "false";

    /**
     * 发送邮箱
     *
     * @param recipientEmail 收件人邮箱
     * @return 单条数据
     */
    @GetMapping("send")
    public String[] testSendEmail(@RequestParam String recipientEmail) {
        String[] result = { is_false, "发送邮件失败!" };
        String data="";
        //获取到已经启动的通知模版
        SysNotice sysNotice= sysNoticeDao.getStatus(1,1);
        if(sysNotice==null){
            result[1] = "没有启动通知模版!";
        }
        //查出来机器码和授权码出来
        String message=sysNotice.getContext();
        try {
            //执行发送邮箱
            sendEmailImpl.sendMail(recipientEmail,sysNotice.getTheme(),message);
            result[0] = is_true;
            result[1] = "发送邮件成功!";
        } catch (Exception e) {
            log.error("发送邮件失败", e);
        }
        return result;
    }

}